package com.simplegovern.authority.action;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.opensymphony.xwork2.Preparable;
import com.simpleframe.web.action.CrudActionSupport;
import com.simplegovern.authority.SysGroup;
import com.simplegovern.authority.service.GroupService;

@Scope("prototype")
@Namespace("/group")
@Action("/group/group")
public class GroupAction extends CrudActionSupport implements Preparable {
	@Autowired
	private GroupService groupService;
	private SysGroup group;

	@Override
	public String main(){
		return MAIN;
	}
	
	@Override
	public String grid() throws Exception {
		page = groupService.getPage(getSearcher());
		return GRID;
	}
	
	@Override
	public String create()throws Exception {
		super.setSaveMode();
		return EDIT;
	}
	
	@Override
	public String edit() throws Exception {
		group = groupService.getById(group.getId());
		super.setUpdateMode();
		return EDIT;
	}
	
	@Override
	public String save() throws Exception {
		group.setInsertTime(new Date());
		groupService.create(group);
		return success("组织添加成功");
	}
	
	@Override
	public String update() throws Exception {
		groupService.update(group);
		return success("组织修改成功");
	}
	
	public void prepareUpdate() throws Exception {
		group = groupService.getById(getId());
	}
	
	public void validator() throws Exception {
		System.out.println("执行验证");
	}
	
	@Override
	public String delete() throws Exception {
		groupService.deletes(ids);
		addMessage("删除成功");
		return null;
	}
	
	//setters and getters
	
	public void setGroup(SysGroup group) {
		this.group = group;
	}
	
	public SysGroup getGroup() {
		return group;
	}
}
